<?php

namespace app\admin\controller;

use app\models\{Business, PlatClass, Recpeople, PlatSons, Goods, GoodsSpec, Region, BusinessArea, Compent, User as Users, Order as Orders, OrderGoods, Payment, Coupon, Wechat};
use think\Db;
use think\Validate;

class Order extends Base
{

    //待付款
    public function order()
    {
        $results = (new Orders())->order(['times' => 'desc', 'created' => 'desc']);
        $data = $this->request->get();
        if (!empty($data['businessid'])) {
            $results = $results->where('businessid', $data['businessid']);
        }
        if (!empty($data['ordersn'])) {
            $results = $results->where('ordersn', $data['ordersn']);
        }
        $province = (new Region())->where(['level' => 1])->select();
        $city = [];
        $area = [];
        $street = [];
        if (!empty($data['province'])) {
            $city_top = (new Region())->where(['level' => 1, 'area_code' => $data['province']])->value('id') ?? 0;
            $city = (new Region())->where(['level' => 2, 'parent_id' => $city_top])->select();
            $results = $results->where(['province' => $data['province']]);
        }
        if (!empty($data['city'])) {
            $area_top = (new Region())->where(['level' => 2, 'area_code' => $data['city']])->value('id') ?? 0;
            $area = (new Region())->where(['level' => 3, 'parent_id' => $area_top])->select();
            $results = $results->where(['city' => $data['city']]);
        }
        if (!empty($data['area'])) {
            $street_top = (new Region())->where(['level' => 3, 'area_code' => $data['area']])->value('id') ?? 0;
            $street = (new Region())->where(['level' => 4, 'parent_id' => $street_top])->select();
            $results = $results->where(['area' => $data['area']]);
        }
        if (!empty($data['street'])) {
            $results = $results->where(['street' => $data['street']]);
        }
        $results = $results->where('status', 1)->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $result['people'] = (new Recpeople())->where(['id' => $result['putorder']])->find() ?? [];
            $result['business'] = (new Business())->where(['id' => $result['businessid']])->value('title');
            $result['status_name'] = $result->status_text;
            $result['province_name'] = (new Region())->where(['area_code' => $result['province'], 'level' => 1])->value('name');
            $result['city_name'] = (new Region())->where(['area_code' => $result['city'], 'level' => 2])->value('name');
            $result['area_name'] = (new Region())->where(['area_code' => $result['area'], 'level' => 3])->value('name');
            $result['street_name'] = (new Region())->where(['id' => $result['street'], 'level' => 4])->value('name');
            $result['user'] = (new Users())->field('nickName,avatarUrl')->where('id', $result['user_id'])->find();
            $goods = (new OrderGoods())->where(['ordersn' => $result['ordersn']])->select();
            foreach ($goods as $good) {
                $good['images'] = json_decode($good['images'], true);
            }
            $result['good'] = $goods;
        }
        $shop = (new Business())->field('id,title,account')->where('status', 1)->select();
        return view(null, [
            'list' => $results,
            'province' => $province,
            'city' => $city,
            'area' => $area,
            'street' => $street,
            'shop' => $shop
        ]);
    }

    //待服务
    public function order2()
    {
        $results = (new Orders())->order(['times' => 'desc', 'created' => 'desc']);
        $data = $this->request->get();
        if (!empty($data['businessid'])) {
            $results = $results->where('businessid', $data['businessid']);
        }
        if (!empty($data['ordersn'])) {
            $results = $results->where('ordersn', $data['ordersn']);
        }
        $province = (new Region())->where(['level' => 1])->select();
        $city = [];
        $area = [];
        $street = [];
        if (!empty($data['province'])) {
            $city_top = (new Region())->where(['level' => 1, 'area_code' => $data['province']])->value('id') ?? 0;
            $city = (new Region())->where(['level' => 2, 'parent_id' => $city_top])->select();
            $results = $results->where(['province' => $data['province']]);
        }
        if (!empty($data['city'])) {
            $area_top = (new Region())->where(['level' => 2, 'area_code' => $data['city']])->value('id') ?? 0;
            $area = (new Region())->where(['level' => 3, 'parent_id' => $area_top])->select();
            $results = $results->where(['city' => $data['city']]);
        }
        if (!empty($data['area'])) {
            $street_top = (new Region())->where(['level' => 3, 'area_code' => $data['area']])->value('id') ?? 0;
            $street = (new Region())->where(['level' => 4, 'parent_id' => $street_top])->select();
            $results = $results->where(['area' => $data['area']]);
        }
        if (!empty($data['street'])) {
            $results = $results->where(['street' => $data['street']]);
        }
        $results = $results->where('status', 2)->where(['tuikuan' => [0, 3, 4]])->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $result['people'] = (new Recpeople())->where(['id' => $result['putorder']])->find() ?? [];
            $result['business'] = (new Business())->where(['id' => $result['businessid']])->value('title');
            $result['status_name'] = $result->status_text;
            $result['province_name'] = (new Region())->where(['area_code' => $result['province'], 'level' => 1])->value('name');
            $result['city_name'] = (new Region())->where(['area_code' => $result['city'], 'level' => 2])->value('name');
            $result['area_name'] = (new Region())->where(['area_code' => $result['area'], 'level' => 3])->value('name');
            $result['street_name'] = (new Region())->where(['id' => $result['street'], 'level' => 4])->value('name');
            $result['user'] = (new Users())->field('nickName,avatarUrl')->where('id', $result['user_id'])->find();
            $goods = (new OrderGoods())->where(['ordersn' => $result['ordersn']])->select();
            foreach ($goods as $good) {
                $good['images'] = json_decode($good['images'], true);
            }
            $result['good'] = $goods;
        }
        $shop = (new Business())->field('id,title,account')->where('status', 1)->select();
        return view(null, [
            'list' => $results,
            'province' => $province,
            'city' => $city,
            'area' => $area,
            'street' => $street,
            'shop' => $shop
        ]);
    }

    //服务中
    public function order3()
    {
        $results = (new Orders())->order(['times' => 'desc', 'created' => 'desc']);
        $data = $this->request->get();
        if (!empty($data['businessid'])) {
            $results = $results->where('businessid', $data['businessid']);
        }
        if (!empty($data['ordersn'])) {
            $results = $results->where('ordersn', $data['ordersn']);
        }
        $province = (new Region())->where(['level' => 1])->select();
        $city = [];
        $area = [];
        $street = [];
        if (!empty($data['province'])) {
            $city_top = (new Region())->where(['level' => 1, 'area_code' => $data['province']])->value('id') ?? 0;
            $city = (new Region())->where(['level' => 2, 'parent_id' => $city_top])->select();
            $results = $results->where(['province' => $data['province']]);
        }
        if (!empty($data['city'])) {
            $area_top = (new Region())->where(['level' => 2, 'area_code' => $data['city']])->value('id') ?? 0;
            $area = (new Region())->where(['level' => 3, 'parent_id' => $area_top])->select();
            $results = $results->where(['city' => $data['city']]);
        }
        if (!empty($data['area'])) {
            $street_top = (new Region())->where(['level' => 3, 'area_code' => $data['area']])->value('id') ?? 0;
            $street = (new Region())->where(['level' => 4, 'parent_id' => $street_top])->select();
            $results = $results->where(['area' => $data['area']]);
        }
        if (!empty($data['street'])) {
            $results = $results->where(['street' => $data['street']]);
        }
        $results = $results->where('status', 3)->where(['tuikuan' => [0, 3, 4]])->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $result['people'] = (new Recpeople())->where(['id' => $result['putorder']])->find() ?? [];
            $result['business'] = (new Business())->where(['id' => $result['businessid']])->value('title');
            $result['status_name'] = $result->status_text;
            $result['province_name'] = (new Region())->where(['area_code' => $result['province'], 'level' => 1])->value('name');
            $result['city_name'] = (new Region())->where(['area_code' => $result['city'], 'level' => 2])->value('name');
            $result['area_name'] = (new Region())->where(['area_code' => $result['area'], 'level' => 3])->value('name');
            $result['street_name'] = (new Region())->where(['id' => $result['street'], 'level' => 4])->value('name');
            $result['user'] = (new Users())->field('nickName,avatarUrl')->where('id', $result['user_id'])->find();
            $goods = (new OrderGoods())->where(['ordersn' => $result['ordersn']])->select();
            foreach ($goods as $good) {
                $good['images'] = json_decode($good['images'], true);
            }
            $result['good'] = $goods;
        }
        $shop = (new Business())->field('id,title,account')->where('status', 1)->select();
        return view(null, [
            'list' => $results,
            'province' => $province,
            'city' => $city,
            'area' => $area,
            'street' => $street,
            'shop' => $shop
        ]);
    }

    //已完成
    public function order4()
    {
        $results = (new Orders())->order(['times' => 'desc', 'created' => 'desc']);
        $data = $this->request->get();
        if (!empty($data['businessid'])) {
            $results = $results->where('businessid', $data['businessid']);
        }
        if (!empty($data['ordersn'])) {
            $results = $results->where('ordersn', $data['ordersn']);
        }
        $province = (new Region())->where(['level' => 1])->select();
        $city = [];
        $area = [];
        $street = [];
        if (!empty($data['province'])) {
            $city_top = (new Region())->where(['level' => 1, 'area_code' => $data['province']])->value('id') ?? 0;
            $city = (new Region())->where(['level' => 2, 'parent_id' => $city_top])->select();
            $results = $results->where(['province' => $data['province']]);
        }
        if (!empty($data['city'])) {
            $area_top = (new Region())->where(['level' => 2, 'area_code' => $data['city']])->value('id') ?? 0;
            $area = (new Region())->where(['level' => 3, 'parent_id' => $area_top])->select();
            $results = $results->where(['city' => $data['city']]);
        }
        if (!empty($data['area'])) {
            $street_top = (new Region())->where(['level' => 3, 'area_code' => $data['area']])->value('id') ?? 0;
            $street = (new Region())->where(['level' => 4, 'parent_id' => $street_top])->select();
            $results = $results->where(['area' => $data['area']]);
        }
        if (!empty($data['street'])) {
            $results = $results->where(['street' => $data['street']]);
        }
        $results = $results->where('status', 4)->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $result['people'] = (new Recpeople())->where(['id' => $result['putorder']])->find() ?? [];
            $result['business'] = (new Business())->where(['id' => $result['businessid']])->value('title');
            $result['status_name'] = $result->status_text;
            $result['province_name'] = (new Region())->where(['area_code' => $result['province'], 'level' => 1])->value('name');
            $result['city_name'] = (new Region())->where(['area_code' => $result['city'], 'level' => 2])->value('name');
            $result['area_name'] = (new Region())->where(['area_code' => $result['area'], 'level' => 3])->value('name');
            $result['street_name'] = (new Region())->where(['id' => $result['street'], 'level' => 4])->value('name');
            $result['user'] = (new Users())->field('nickName,avatarUrl')->where('id', $result['user_id'])->find();
            $goods = (new OrderGoods())->where(['ordersn' => $result['ordersn']])->select();
            foreach ($goods as $good) {
                $good['images'] = json_decode($good['images'], true);
            }
            $result['good'] = $goods;
        }
        $shop = (new Business())->field('id,title,account')->where('status', 1)->select();
        return view(null, [
            'list' => $results,
            'province' => $province,
            'city' => $city,
            'area' => $area,
            'street' => $street,
            'shop' => $shop
        ]);
    }

    //已取消
    public function order5()
    {
        $results = (new Orders())->order(['times' => 'desc', 'created' => 'desc']);
        $data = $this->request->get();
        if (!empty($data['businessid'])) {
            $results = $results->where('businessid', $data['businessid']);
        }
        if (!empty($data['ordersn'])) {
            $results = $results->where('ordersn', $data['ordersn']);
        }
        $province = (new Region())->where(['level' => 1])->select();
        $city = [];
        $area = [];
        $street = [];
        if (!empty($data['province'])) {
            $city_top = (new Region())->where(['level' => 1, 'area_code' => $data['province']])->value('id') ?? 0;
            $city = (new Region())->where(['level' => 2, 'parent_id' => $city_top])->select();
            $results = $results->where(['province' => $data['province']]);
        }
        if (!empty($data['city'])) {
            $area_top = (new Region())->where(['level' => 2, 'area_code' => $data['city']])->value('id') ?? 0;
            $area = (new Region())->where(['level' => 3, 'parent_id' => $area_top])->select();
            $results = $results->where(['city' => $data['city']]);
        }
        if (!empty($data['area'])) {
            $street_top = (new Region())->where(['level' => 3, 'area_code' => $data['area']])->value('id') ?? 0;
            $street = (new Region())->where(['level' => 4, 'parent_id' => $street_top])->select();
            $results = $results->where(['area' => $data['area']]);
        }
        if (!empty($data['street'])) {
            $results = $results->where(['street' => $data['street']]);
        }
        $results = $results->where('status', 5)->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $result['people'] = (new Recpeople())->where(['id' => $result['putorder']])->find() ?? [];
            $result['business'] = (new Business())->where(['id' => $result['businessid']])->value('title');
            $result['status_name'] = $result->status_text;
            $result['province_name'] = (new Region())->where(['area_code' => $result['province'], 'level' => 1])->value('name');
            $result['city_name'] = (new Region())->where(['area_code' => $result['city'], 'level' => 2])->value('name');
            $result['area_name'] = (new Region())->where(['area_code' => $result['area'], 'level' => 3])->value('name');
            $result['street_name'] = (new Region())->where(['id' => $result['street'], 'level' => 4])->value('name');
            $result['user'] = (new Users())->field('nickName,avatarUrl')->where('id', $result['user_id'])->find();
            $goods = (new OrderGoods())->where(['ordersn' => $result['ordersn']])->select();
            foreach ($goods as $good) {
                $good['images'] = json_decode($good['images'], true);
            }
            $result['good'] = $goods;
        }
        $shop = (new Business())->field('id,title,account')->where('status', 1)->select();
        return view(null, [
            'list' => $results,
            'province' => $province,
            'city' => $city,
            'area' => $area,
            'street' => $street,
            'shop' => $shop
        ]);
    }

    //商家售后订单
    public function order6()
    {
        $results = (new Orders())->order(['times' => 'desc', 'created' => 'desc']);
        $data = $this->request->get();
        if (!empty($data['businessid'])) {
            $results = $results->where('businessid', $data['businessid']);
        }
        if (!empty($data['ordersn'])) {
            $results = $results->where('ordersn', $data['ordersn']);
        }
        $province = (new Region())->where(['level' => 1])->select();
        $city = [];
        $area = [];
        $street = [];
        if (!empty($data['province'])) {
            $city_top = (new Region())->where(['level' => 1, 'area_code' => $data['province']])->value('id') ?? 0;
            $city = (new Region())->where(['level' => 2, 'parent_id' => $city_top])->select();
            $results = $results->where(['province' => $data['province']]);
        }
        if (!empty($data['city'])) {
            $area_top = (new Region())->where(['level' => 2, 'area_code' => $data['city']])->value('id') ?? 0;
            $area = (new Region())->where(['level' => 3, 'parent_id' => $area_top])->select();
            $results = $results->where(['city' => $data['city']]);
        }
        if (!empty($data['area'])) {
            $street_top = (new Region())->where(['level' => 3, 'area_code' => $data['area']])->value('id') ?? 0;
            $street = (new Region())->where(['level' => 4, 'parent_id' => $street_top])->select();
            $results = $results->where(['area' => $data['area']]);
        }
        if (!empty($data['street'])) {
            $results = $results->where(['street' => $data['street']]);
        }
        $results = $results->where(['status' => [2, 3], 'shouhou' => 2])->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $result['people'] = (new Recpeople())->where(['id' => $result['putorder']])->find() ?? [];
            $result['business'] = (new Business())->where(['id' => $result['businessid']])->value('title');
            $result['status_name'] = $result->status_text;
            $result['province_name'] = (new Region())->where(['area_code' => $result['province'], 'level' => 1])->value('name');
            $result['city_name'] = (new Region())->where(['area_code' => $result['city'], 'level' => 2])->value('name');
            $result['area_name'] = (new Region())->where(['area_code' => $result['area'], 'level' => 3])->value('name');
            $result['street_name'] = (new Region())->where(['id' => $result['street'], 'level' => 4])->value('name');
            $result['user'] = (new Users())->field('nickName,avatarUrl')->where('id', $result['user_id'])->find();
            $goods = (new OrderGoods())->where(['ordersn' => $result['ordersn']])->select();
            foreach ($goods as $good) {
                $good['images'] = json_decode($good['images'], true);
            }
            $result['good'] = $goods;
        }
        $shop = (new Business())->field('id,title,account')->where('status', 1)->select();
        return view(null, [
            'list' => $results,
            'province' => $province,
            'city' => $city,
            'area' => $area,
            'street' => $street,
            'shop' => $shop
        ]);
    }

    //平台待退款订单
    public function order7()
    {
        $results = (new Orders())->order(['times' => 'desc', 'created' => 'desc']);
        $data = $this->request->get();
        if (!empty($data['businessid'])) {
            $results = $results->where('businessid', $data['businessid']);
        }
        if (!empty($data['ordersn'])) {
            $results = $results->where('ordersn', $data['ordersn']);
        }
        $province = (new Region())->where(['level' => 1])->select();
        $city = [];
        $area = [];
        $street = [];
        if (!empty($data['province'])) {
            $city_top = (new Region())->where(['level' => 1, 'area_code' => $data['province']])->value('id') ?? 0;
            $city = (new Region())->where(['level' => 2, 'parent_id' => $city_top])->select();
            $results = $results->where(['province' => $data['province']]);
        }
        if (!empty($data['city'])) {
            $area_top = (new Region())->where(['level' => 2, 'area_code' => $data['city']])->value('id') ?? 0;
            $area = (new Region())->where(['level' => 3, 'parent_id' => $area_top])->select();
            $results = $results->where(['city' => $data['city']]);
        }
        if (!empty($data['area'])) {
            $street_top = (new Region())->where(['level' => 3, 'area_code' => $data['area']])->value('id') ?? 0;
            $street = (new Region())->where(['level' => 4, 'parent_id' => $street_top])->select();
            $results = $results->where(['area' => $data['area']]);
        }
        if (!empty($data['street'])) {
            $results = $results->where(['street' => $data['street']]);
        }
        $results = $results->where(['status' => [2, 3], 'shouhou' => 2, 'tuikuan' => 2])->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $result['people'] = (new Recpeople())->where(['id' => $result['putorder']])->find() ?? [];
            $result['business'] = (new Business())->where(['id' => $result['businessid']])->value('title');
            $result['status_name'] = $result->status_text;
            $result['province_name'] = (new Region())->where(['area_code' => $result['province'], 'level' => 1])->value('name');
            $result['city_name'] = (new Region())->where(['area_code' => $result['city'], 'level' => 2])->value('name');
            $result['area_name'] = (new Region())->where(['area_code' => $result['area'], 'level' => 3])->value('name');
            $result['street_name'] = (new Region())->where(['id' => $result['street'], 'level' => 4])->value('name');
            $result['user'] = (new Users())->field('nickName,avatarUrl')->where('id', $result['user_id'])->find();
            $result['payorder'] = (new Payment())->where(['type' => 1, 'status' => 2, 'ordersn' => $result['ordersn']])->order('id desc')->value('paymentid') ?? '';
            $goods = (new OrderGoods())->where(['ordersn' => $result['ordersn']])->select();
            foreach ($goods as $good) {
                $good['images'] = json_decode($good['images'], true);
            }
            $result['good'] = $goods;
        }
        $shop = (new Business())->field('id,title,account')->where('status', 1)->select();
        return view(null, [
            'list' => $results,
            'province' => $province,
            'city' => $city,
            'area' => $area,
            'street' => $street,
            'shop' => $shop
        ]);
    }

    public function order_str()
    {
        $data = $this->request->post();
        if (empty($data['id']) || empty($data['status'])) {
            return  json(['status' => 1, 'code' => 1, 'msg' => '信息参数错误!']);
        }
        $order = (new Orders())->where('id', $data['id'])->find();
        if (empty($order)) {
            return  json(['status' => 1, 'code' => 1, 'msg' => '订单不存在!']);
        }
        if ($order['status'] == 4 || $order['status'] == 5) {
            return json(['status' => 1, 'code' => 1, 'msg' => '订单已完成或取消，无法继续操作!']);
        }
        switch ($data['status']) {
            case '2':
                break;
                //完成订单
            case '4':
                if ($order['status'] != 2 && $order['status'] != 3) {
                    return json(['status' => 1, 'code' => 1, 'msg' => '订单状态不可完成!']);
                }
                $res = (new Orders())->where('id', $data['id'])->update(['status' => 4, 'finishtimes' => date('Y-m-d H:i:s')]);
                if ($res) {
                    return  json(['status' => 0, 'code' => 0, 'msg' => '订单已完成!']);
                }
                break;
                //取消订单
            case '5':
                //未支付订单直接取消
                if ($order['status'] == 1) {
                    Db::startTrans();
                    try {
                        $res = (new Orders())->where('id', $data['id'])->update(['status' => 5, 'caceltimes' => date('Y-m-d H:i:s')]);
                        if ($order['coupon'] > 0) {
                            (new Coupon())->where('id', $order['coupon'])->update(['status' => 1]);
                        }
                        Db::commit();
                    } catch (\Exception $e) {
                        Db::rollback();
                        return json(['status' => 1, 'code' => 1, 'msg' => '操作失败，请重试!']);
                    }
                    if ($res) {
                        return json(['status' => 0, 'code' => 0, 'msg' => '订单已取消!']);
                    }
                    return json(['status' => 1, 'code' => 1, 'msg' => '操作失败，请重试!']);
                }
                //已支付订单递交平台处理退款等操作
                if ($order['status'] == 2 || $order['status'] == 3) {
                    $res = (new Orders())->where('id', $data['id'])->update(['tuikuan' => 2, 'shouhou' => 2]);
                    if ($res) {
                        return  json(['status' => 0, 'code' => 0, 'msg' => '订单已提交平台退款处理!']);
                    }
                }
                //商家拒绝退款
            case '99':
                $res = (new Orders())->where('id', $data['id'])->update(['shouhou' => 1, 'tuikuan' => 3]);
                if ($res) {
                    return  json(['status' => 0, 'code' => 0, 'msg' => '订单已拒绝!']);
                }
                break;
                //商家处理退款
            case '100':
                if ($order['total'] <= 0) {
                    Db::startTrans();
                    try {
                        $res = (new Orders())->where('id', $data['id'])->update(['tuikuan' => 2, 'status' => 5]);
                        if ($order['coupon'] > 0) {
                            (new Coupon())->where('id', $order['coupon'])->update(['status' => 1]);
                        }
                        Db::commit();
                    } catch (\Exception $e) {
                        Db::rollback();
                        return json(['status' => 1, 'code' => 1, 'msg' => '操作失败，请重试!']);
                    }
                    return  json(['status' => 0, 'code' => 0, 'msg' => '订单已取消!']);
                } else {
                    $res = (new Orders())->where('id', $data['id'])->update(['tuikuan' => 2]);
                }
                if ($res) {
                    return  json(['status' => 0, 'code' => 0, 'msg' => '订单已处理!']);
                }
                break;
                //平台拒绝退款
                // case '101':
                //     $res = (new Orders())->where('id', $data['id'])->update(['tuikuan' => 4]);
                //     if ($res) {
                //         return  json(['status' => 0, 'code' => 0, 'msg' => '订单已拒绝退款!']);
                //     }
                //     break;
                // case '102':
                //     //同意退款 查找退款订单号
                //     if ($order['total'] > 0) {
                //         $payment = (new Payment())->where(['status' => 2, 'ordersn' => $order['ordersn']])->find();
                //         Db::startTrans();
                //         try {
                //             (new Orders())->where('id', $data['id'])->update(['shouhou' => 1, 'status' => 5, 'caceltimes' => date('Y-m-d H:i:s')]);
                //             if ($order['coupon'] > 0) {
                //                 (new Coupon())->where('id', $order['coupon'])->update(['status' => 1]);
                //             }
                //             Db::commit();
                //         } catch (\Exception $e) {
                //             Db::rollback();
                //             return json(['status' => 1, 'code' => 1, 'msg' => '操作失败，请重试!']);
                //         }
                //     }

                //     return  json(['status' => 0, 'code' => 0, 'msg' => '订单已处理!']);
                //     break;
            default:
                return json(['status' => 1, 'code' => 1, 'msg' => '操作状态出错!']);
                break;
        }
    }

    public function order_send()
    {
        $data = $this->request->post();
        if (empty($data['id'])) {
            return  json(['status' => 1, 'code' => 1, 'msg' => '信息参数错误!']);
        }
        $order = (new Orders())->where('id', $data['id'])->find();
        if (empty($order)) {
            return  json(['status' => 1, 'code' => 1, 'msg' => '订单不存在!']);
        }
        if ($order['status'] != 2 && $order['status'] != 3) {
            return json(['status' => 1, 'code' => 1, 'msg' => '订单状态不可指派或发货!']);
        }
        //查找技工账号
        if ($order['type'] == 1 || $order['type'] == 2 || $order['type'] == 3) {
            if (empty($data['value'])) {
                $res = (new Orders())->where('id', $data['id'])->update(['status' => 3, 'fwtimes' => date('Y-m-d H:i:s')]);
                if ($res) {
                    return  json(['status' => 0, 'code' => 0, 'msg' => '接单成功!']);
                }
            } else {
                $people = (new Recpeople())->where(['mobile' => $data['value'], 'business' => $order['businessid']])->find();
                if (empty($people)) {
                    return json(['status' => 1, 'code' => 1, 'msg' => '技师不存在!']);
                }
                $res = (new Orders())->where('id', $data['id'])->update(['status' => 3, 'putorder' => $people['id'], 'fwtimes' => date('Y-m-d H:i:s')]);
                if ($res) {
                    return  json(['status' => 0, 'code' => 0, 'msg' => '指派成功!']);
                }
            }
        }
        if ($order['type'] == 4) {
            if (empty($data['value']) || empty($data['value2'])) {
                return  json(['status' => 1, 'code' => 1, 'msg' => '缺少信息参数!']);
            }
            $res = (new Orders())->where('id', $data['id'])->update(['status' => 3, 'fwtimes' => date('Y-m-d H:i:s'), 'express' => $data['value'], 'express_num' => $data['value2']]);
            if ($res) {
                return  json(['status' => 0, 'code' => 0, 'msg' => '发货成功!']);
            }
        }
        return  json(['status' => 1, 'code' => 1, 'msg' => '操作失败，请重试!']);
    }


    public function orderdetail()
    {
        $ordersn = $this->request->get('ordersn/d', 0);
        $list = (new OrderGoods())->where(['ordersn' => $ordersn])->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($list as $lists) {
            $lists['images'] = json_decode($lists['images'], true);
        }
        return view(null, [
            'list' => $list
        ]);
    }
}